package sia.erp.venta_ruta;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainMenuActivity extends ListActivity implements OnItemClickListener{
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Utils.isConnected = isNetworkAvailable();
        
        
        showDialog();
        
        //Obtiene el array de los recursos
        final String Menu[] = getResources().getStringArray(R.array.Menu);
        
        //El ListAdapter para la lista del menu
        setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main_menu, Menu));
        
        //Obtiene el ListView
    	ListView lstOpciones = getListView();
    	
    	//Para cuando se hace "click en una opcion
    	lstOpciones.setOnItemClickListener(this);
    }
    

	public void onItemClick(AdapterView<?> parent, View view, int pos,
			long id) {
		switch(pos){
			case 0:
				Intent Articulos = new Intent(this, VerArticulosActivity.class);
				this.startActivity(Articulos);
				break;
			case 1:
				Intent OrdenCompra = new Intent(this, OrdenCompraActivity.class);
				this.startActivity(OrdenCompra);
				break;
			case 2:
				Intent Consulta1 = new Intent(this, ConsultaDetalleArticulo.class);
				this.startActivity(Consulta1);
				break;
			case 3:
				Intent Consulta2 = new Intent(this, ConsultaUltimaVenta.class);
				this.startActivity(Consulta2);
				break;
			case 4:
				Intent Consulta3 = new Intent(this, ConsultaCliente.class);
				this.startActivity(Consulta3);
				break;
		}
	}
    
	public void showDialog(){
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Empresa:");
        alert.setMessage("Nombre");

        // Set an EditText view to get user input 
        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          String value = input.getText().toString();
          Utils.nombre_empresa = value;
          
          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
        	  
          }
        });

        alert.show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main_menu, menu);
		return true;
	}
	
	private boolean isNetworkAvailable() {
	    ConnectivityManager connectivityManager 
	          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
	    return activeNetworkInfo != null;
	}
}